<template>
	<div id="user">
		<h3 style="padding:0 0 15px 15px">{{ isEdit ? "编辑用户" : "添加用户"}}</h3>
		<el-form :rules="rules" ref="form" :model="form" label-width="80px">
			<el-form-item
					v-for="(field, index) in fields"
					v-if="!field.noSave &&!field.noFormDisplay"
					:label="field.label"
					:prop="field.tableName"
					:key="index"
					>
				<wt-model-field-input
						  :ref="field.tableName"
						  :field="field"
						  :form="form"
						  :params-data="paramsData"
						  :class="{'wt-model-inline-field' : field.conditions}"></wt-model-field-input>
				<span class="wt-extra" v-if="field.extra">{{field.extra}}</span>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="onSave" :loading="saving" icon="check">保存</el-button>
				<el-button @click="onCancel" icon="close">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>
<script>
	import Vue from 'vue'
	import ElementUI from 'element-ui'
	import request from 'winged/utils/axios'
	import Fields from 'winged/utils/wt-model-field'
	import Permissions from 'winged/utils/wt-permission'
	import FieldInput from 'winged/components/wt-model-field-input'
	import ModelUtils from 'winged/utils/wt-model-utils'
	import ModelService from 'winged/utils/wt-model-service'
	import WtDepartmentSelect from 'winged/components/wt-department-select'
	import FieldSelect from 'winged/components/wt-model-field-select'
	Vue.use(ElementUI)
	Vue.component(WtDepartmentSelect.name, WtDepartmentSelect)
	Vue.component(FieldSelect.name, FieldSelect);
	export default{
		name:'wt-new-user',
		props:{
			model: {
				type: Object,
				required: true,
			},
			value: {
				type: Object,
				default: {}
			},
			id: {
				type: [Object, String, Number],
				default: null
			},
			paramsData: {
				type: Object,
				default: null
			},
		},
		data(){
			var valPass = (rule, value, callback) => {//正则检验密码
				if (/^[\w~`!@#$%^&*\(\)_\-+=\{\}\[\]|;:<>.\?\/]{6,20}$/.test( value )){
					callback()
				}else {
					callback(new Error('密码必须为6-20个字符，只能包含大小写字母、数字以及标点符号（空格、引号以及逗号除外）'));
				}
			};
			var valuSer = (rule, value, callback) => {//正则检验用户名
				if (/^[\w]{4,20}$/.test( value )){
					callback()
				}else {
					callback(new Error('用户名必须为4-20个字符，可以包含数字、字母和下划线，不支持其他特殊字符'));
				}
			};
			return{
				form: this.value != null ? ModelUtils.cloneObject(this.value) : ModelUtils.createDefaultObject(this.model),
				field:{
					'optionsParam':'roles'
				},
				fields:this.model.fields,
				isEdit: true,
				saving:false,
				rules:{
					username: [
						{  required: true, message: '请输入用户名', trigger: 'blur' },
						{ validator: valuSer, trigger: 'blur' }
					],
					password: [
						{  required: true, message: '请输入密码', trigger: 'blur' },
						{ validator: valPass, trigger: 'blur' }
					],
					name: [
						{ required: true, message: '请输入医生姓名', trigger: 'blur' }
					],
					second_department_id: [
						{ required: true, message: '请选择科室', trigger: 'change' }
					],
					roles: [
						{ type: 'array', required: true, message: '请至少选择一个角色', trigger: 'blur' }
					]
				}
			}
		},
		computed: {
		},
		methods:{
			onSave(){
				this.doSave();
			},
			onCancel(){
				this.$emit('cancel');
			},
			doSave() {
				this.$refs.form.validate((valid) => {
					if (valid) {
						this.saving = true;
						let config;
						if (this.isEdit) {
							config = this.model.update;
						} else {
							config = this.model.new;
						}
						this.rolesGrade();
						ModelUtils.buildQueryExtraParams(config, this.paramsData, this.form);
						ModelService.save(this.model, this.id, this.form, (data) => {
							this.$emit('saved', this.form);
							this.saving = false;
							this.$message.success('保存成功');
						}, (error) => {
							if (error.response.data.msg.indexOf('USER_EXIST')>-1){
								this.$message.error('该用户为已有用户,请确认用户名');
							}else {
								this.$message.error('保存失败');
							}
							this.saving = false;
						})
					}
				})
			},
			rolesGrade(){
				let roles = this.form.roles;
				let superAdmin = false;
				let sec = false;
				let array = [];
				for (let a=0;a<roles.length;a++){
					if (typeof(roles[a]) == 'object'){
//						if (roles[a].slug == 'master-superadmin'){
//							superAdmin = true;
//							break;
//						}else if (roles[a].slug == 'tenant-admin'){
//							sec = true;
//							break;
//						}else {
//							array.push(roles[a].id);
//						}
						array.push(roles[a].id);
					}else {
						array = roles;
					}
				}
//				if (superAdmin == true){
//					let role = this.paramsData.roles;
//					let arr = [];
//					for (let i=0;i<role.length;i++){
//						arr.push(role[i].id)
//					}
//					this.form.roles = arr;
//				}else if (sec == true){
//					let role = this.paramsData.roles;
//					let arr = [];
//					for (let i=0;i<role.length;i++){
//						if (role[i].slug == 'master-superadmin'){
//							continue;
//						}
//						arr.push(role[i].id)
//					}
//					this.form.roles = arr;
//				}else {
//					this.form.roles = array;
//				}
				this.form.roles = array;
			},
		},
		created(){
			if (this.id != null){
				this.isEdit = true;
				for(let i in this.fields){
					if (this.fields[i].label == '用户名'||this.fields[i].label == '密码'){
						this.fields[i].noSave = true
					}
				}
			}else {
				this.isEdit = false;
				for(let i in this.fields){
					if ((this.fields[i].label == '用户名'&&this.fields[i].display==false)||this.fields[i].label == '密码'){
						this.fields[i].noSave = false
					}
				}
			}
		}
	}
</script>
<style>
	.el-form{
		max-width: 800px;
	}
	.input-max-width .el-input__inner{
		max-width: 360px;
	}
	.el-select .el-tag__close{
		margin: 0!important;
	}
	.el-tag .el-icon-close{
		top: 0!important;
	}
	.wt-model-field-input{
		display: inline-block;
	}
	.wt-extra{
		color: #555;
	}

</style>